package com.hinacom.pix.web.controller;

import com.hinacom.pix.web.vo.PixData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

/**
 * Created by fyu on 2016/8/24.
 */
@Controller
@RequestMapping("v3-convert")
public class V3ConvertController {
    @RequestMapping("")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        PixData vo = new PixData();
        ModelAndView mv = new ModelAndView("v3-convert");
        mv.addObject("vo", vo);
        return mv;
    }

    @RequestMapping("send")
    public ModelAndView send(String input) {

        SOAPConnectionFactory soapConnFactory = null;
        try {
            soapConnFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection connection = soapConnFactory.createConnection();

            //  创建消息对象
            // ===========================================
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage message = messageFactory.createMessage();
            SOAPPart soapPart = message.getSOAPPart();// 创建soap部分



        } catch (SOAPException e) {
            e.printStackTrace();
        }




        PixData vo = new PixData();
        vo.setInput(input);


        ResourceBundle bundle = PropertyResourceBundle.getBundle("testtool");
        String host = bundle.getString("convert-host");
        String pixPort = bundle.getString("convert-port");
        int port = Integer.parseInt(pixPort);

        Socket socket = null;
        try {
            socket = new Socket(host, port);
            socket.setSoLinger(true, 1000);

            InputStream msgInputStream = new ByteArrayInputStream(input.getBytes("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close(); //关闭Socket
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        ModelAndView mv = new ModelAndView("v3-convert");
        mv.addObject("vo", vo);
        return mv;
    }
}
